package cn.how2go.net.server.service;

import cn.how2go.net.server.base.LockList;
import cn.how2go.net.server.base.SessionData;
import cn.how2go.net.server.bean.ByteMessage;
import cn.how2go.net.server.protocol.JTBytes;
import cn.how2go.net.server.vo.CtxMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CtxMessageService {
    private static Logger logger = LoggerFactory.getLogger(CtxMessageService.class);
    public void read(){
        CtxMessage ctxMessage = null;
        try{
            synchronized (LockList.decoderLock){
                if(SessionData.RECEIVE_MESSAGE.size()<=0){
                    LockList.decoderLock.wait();
                }
                ctxMessage = SessionData.RECEIVE_MESSAGE.poll();
                byte[] response = decoderMessage(ctxMessage.getMessage());
                if(response != null && ctxMessage.getCtx()!=null) {
                    try{
                        ctxMessage.getCtx().channel().writeAndFlush(response);
                        ByteMessageService.write(new ByteMessage("S",response));
                    }catch (Exception e){
                        logger.error("ctx<<<<<<发送失败");
                    }
                }
                LockList.decoderLock.notify();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private byte[] decoderMessage(JTBytes jtBytes){
        //回复的字节报文
        byte[] response = null;
//        //获取消息ID的字节数组
//        short msgType = jtBytes.getHeader().getMsgType();
//        //消息类型转换
//        MessageType type = MessageType.getByValue(msgType);
//        try {
//            switch (type) {
//                case JT_0001:
//                    response = ProtocolService.protocolJT0001(jtBytes);
//                    break;
//                case JT_0002:
//                    response = ProtocolService.createJT8001(jtBytes,(byte)0x00);
//                    break;
//                case JT_0003:
//                    break;
//                case JT_0100:
//                    response = ProtocolService.protocolJT0100(jtBytes);
//                    break;
//                case JT_0102:
//                    response = ProtocolService.protocolJT0102(jtBytes);
//                    break;
//                case JT_0200:
//                    response = ProtocolService.protocolJT0200(jtBytes);
//                    break;
//                case JT_0704:
//                    response = ProtocolService.protocolJT0704(jtBytes);
//                    break;
//                default:
//            }
//        }catch (Exception e){
//            logger.error("error msg<<<<<<<<<<"+e.getMessage());
//            //解析出错的消息保存到日志中
//            logger.info(jtBytes.toString());
//            response = ProtocolService.createJT8001(jtBytes,(byte) 0x01);
//        }
        return response;
    }

    /**
     * 向报文队列写入报文
     * @param ctxMessage
     */
    public static void write(CtxMessage ctxMessage){
        try{
            synchronized (LockList.decoderLock){
                if(SessionData.RECEIVE_MESSAGE.size()>0){
                    LockList.decoderLock.wait();
                }
                SessionData.RECEIVE_MESSAGE.offer(ctxMessage);
                LockList.decoderLock.notify();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
